<template>
  <div v-loading="loading">
    <div class="header-search cad-box">
      <el-input
        v-model="searchValue.realName"
        style="width: 20%"
        clearable
        placeholder="请输入调研名称"
      />
      <el-select
        v-model="searchValue.status"
        placeholder="请选择状态"
        style="width: 20%; margin: 0px 10px"
        clearable
      >
        <el-option
          v-for="item in options1"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button
        type="primary"
        style="margin: 0px 10px"
        @click="handleResearchTemplateList"
      >搜 索</el-button>
    </div>

    <div class="content-table cad-box">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="name" label="调研模版名称" align="center" />
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            {{ options1.find((item) => item.value === scope.row.status).label }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center" />
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handleInfo(scope.row, 1)"
            >详情</el-button>
            <el-button
              v-if="scope.row.status !== 1"
              type="text"
              size="small"
              @click="handleInfo(scope.row, 2)"
            >编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="text-align: right">
        <el-pagination
          :current-page="pagination.pageNum"
          :page-sizes="[10, 15, 30, 50]"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { researchTemplateList } from './api'
export default {
  data () {
    return {
      options1: [
        {
          value: 0,
          label: '未提交'
        },
        {
          value: 1,
          label: '已提交'
        }
      ],
      options2: [
        {
          value: 1,
          label: '安全'
        },
        {
          value: 2,
          label: '卫生'
        },
        {
          value: 3,
          label: '环境'
        }
      ],
      value1: '',
      value2: '',
      drawer: false,
      direction: 'rtl',
      infoItem: {},
      loading: false,
      searchValue: {
        realName: '',
        status: ''
      },
      tableData: [],
      pagination: {
        pageNum: 0,
        pageSize: 10
      },
      total: 0
    }
  },
  computed: {
    ...mapGetters(['curArea', 'user'])
  },
  created () {
    this.handleResearchTemplateList()
  },
  mounted () {},
  methods: {
    async handleResearchTemplateList () {
      this.loading = true
      const res = await researchTemplateList({
        pageNum: this.pagination.pageNum,
        pageSizes: this.pagination.pageSize,
        areaId: this.curArea.id,
        name: this.searchValue.realName,
        status: this.searchValue.status
      })
      this.loading = false
      this.tableData = res.records
      this.total = Number(res.total)
    },
    handleInfo (item, type) {
      if (type === 1) {
        this.$router.push(`/survey/editDetail?id=${item.id}&type=info`)
      } else {
        this.$router.push(`/survey/editDetail?id=${item.id}&type=edit`)
      }
    },
    onSubmit () {},
    getTableList () {},
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.pagination.pageSize = val
      this.handleResearchTemplateList()
    },

    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.pagination.pageNum = val
      this.handleResearchTemplateList()
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-drawer__header {
  color: #000 !important;
  font-size: 18px !important;
}

.header-search {
  display: flex;
  margin-bottom: 30px;

  .select-box {
    width: 20%;
    margin: 0 20px;
  }
}

.drawer_box {
  width: 100%;
  padding: 0px 50px;
  .drawer_item {
    display: flex;
    justify-content: flex-start;
    margin-bottom: 20px;
    padding: 10px;
  }
}
</style>
